﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Scan
{
    public partial class BaseView : UserControl
    {
        public BaseView()
        {
            InitializeComponent();
        }

        #region 基类窗体_属性

        // 保存返回的视图
        private BaseView resultView;
        public BaseView ResultView
        {
            get { return resultView; }
            set { resultView = value; }
        }

        /// <summary>
        /// 当前界面是否能扫描
        /// </summary>
        private bool acceptScan;
        public bool AcceptScan
        {
            get { return acceptScan; }
            set { acceptScan = value; }
        }

        /// <summary>
        /// 标题
        /// </summary>
        protected string title;
        public string Title
        {
            get { return title; }
            set { title = value;}
        }

        /// <summary>
        /// 状态信息提示
        /// </summary>
        protected string status;
        public string Status
        {
            get { return status; }
            set { status = value; }
        }

        // 清空控件
        protected bool needClearView = true;
        public bool NeedClearView
        {
            get { return needClearView; }
            set { needClearView = value; }
        }

        // 传输类型
        public TransType TransferType
        {
            get;
            set;
        }

        // 传输时的标题
        public string TransferTitle
        {
            get;
            set;
        }

        /// <summary>
        /// 父窗体
        /// </summary>
        private MainForm parentForm;
        public MainForm ParentForm
        {
            get { return parentForm; }
            set { parentForm = value; }
        }
        #endregion

        protected void DoFocusControl(ControlCollection controls, bool forward)
        {

            foreach (Control SubControl in controls)
            {
                if (SubControl.Controls.Count > 0)
                {

                    DoFocusControl(SubControl.Controls, forward);
                }
                else if (SubControl.Focused)
                {

                    this.SelectNextControl(SubControl, forward, true, true, true);
                    break;
                }
            }
        }

        protected virtual void FocusControl(bool forward)
        {
            DoFocusControl(this.Controls, forward);
            /*
            foreach (Control control in this.Controls)
            {
                if (control.Focused)
                {
                    this.SelectNextControl(control, forward, true, true, true);
                    break;
                }
            }
             */
        }

        /// <summary>
        /// 加载时触发事件
        /// </summary>
        public virtual void OnLoad() { }

        /// <summary>
        /// 卸载时触发事件
        /// </summary>
        public virtual void OnUnload() { }

        /// <summary>
        /// 窗体返回时的回调方法
        /// </summary>
        public virtual void ReturnProc(Boolean Successed, ReferType ReferValue, object ParamObject) { }

        /// <summary>
        /// 获取到条码
        /// </summary>
        /// <param name="scanStr"></param>
        public virtual void OnGetBarcode(string scanStr) { }

        /// <summary>
        /// 单击快捷键按下后激活事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void OnHotKey(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    this.FocusControl(false);
                    break;
                case Keys.Down:
                    this.FocusControl(true);
                    break;
                case Keys.NumPad1:
                    NumPad1(this, e);
                    break;
                case Keys.NumPad2:
                    NumPad2(this, e);
                    break;
                case Keys.NumPad3:
                    NumPad3(this, e);
                    break;
                case Keys.NumPad4:
                    NumPad4(this, e);
                    break;
                case Keys.NumPad5:
                    NumPad5(this, e);
                    break;
                case Keys.NumPad6:
                    NumPad6(this, e);
                    break;
                case Keys.NumPad7:
                    NumPad7(this, e);
                    break;
                case Keys.NumPad8:
                    NumPad8(this, e);
                    break;
                case Keys.NumPad9:
                    NumPad9(this, e);
                    break;
                case Keys.F11:
                    S1(this, e);
                    break;
                case Keys.F12:
                    S2(this, e);
                    break;
                case Keys.F1:
                    F1(this, e);
                    break;
                case Keys.F2:
                    F2(this, e);
                    break;
                case Keys.F3:
                    F3(this, e);
                    break;
                case Keys.F4:
                    F4(this, e);
                    break;
                case Keys.F5:
                    F5(this, e);
                    break;
                case Keys.F6:
                    F6(this, e);
                    break;
                case Keys.F7:
                    F7(this, e);
                    break;
                case Keys.F8:
                    F8(this, e);
                    break;
                case Keys.F9:
                    F9(this, e);
                    break;
                case Keys.Enter:
                    Enter(this, e);
                    break;
                case Keys.Escape:
                    Escape(this, e);
                    break;
            }
        }

        #region 特定快捷键事件

        public virtual void S1(object sender, EventArgs e) { }

        public virtual void S2(object sender, EventArgs e) { }

        public virtual void NumPad1(object sender, EventArgs e) { }

        public virtual void NumPad2(object sender, EventArgs e) { }

        public virtual void NumPad3(object sender, EventArgs e) { }

        public virtual void NumPad4(object sender, EventArgs e) { }

        public virtual void NumPad5(object sender, EventArgs e) { }

        public virtual void NumPad6(object sender, EventArgs e) { }

        public virtual void NumPad7(object sender, EventArgs e) { }

        public virtual void NumPad8(object sender, EventArgs e) { }

        public virtual void NumPad9(object sender, EventArgs e) { } 

        public virtual void F1(object sender, EventArgs e) { }

        public virtual void F2(object sender, EventArgs e) { }

        public virtual void F3(object sender, EventArgs e) { }

        public virtual void F4(object sender, EventArgs e) { }

        public virtual void F5(object sender, EventArgs e) { }

        public virtual void F6(object sender, EventArgs e) { }

        public virtual void F7(object sender, EventArgs e) { }

        public virtual void F8(object sender, EventArgs e) { }

        public virtual void F9(object sender, EventArgs e) 
        {
            this.parentForm.Close();
        }

        public virtual void Enter(object sender, EventArgs e) { }

        public virtual void Escape(object sender, EventArgs e) { }

        #endregion

        #region 网络事件、传输事件

        /// <summary>
        /// 连接网络
        /// </summary>
        public virtual void OnConnectStart()
        {
            GlobalCache.Instance.NetworkMgr.ConnectNotify += new ConnectNotifyEventHandler(ConnectNotify);
            this.ParentForm.ShowProgress(TransferTitle, true);
            this.Enabled = false;
            GlobalCache.Instance.NetworkMgr.Connect(NetworkManager.ConnectType);
        }

        protected void ConnectNotify(object sender, ConnectNotifyEventArgs e)
        {
            if (this.InvokeRequired)
            {
                object[] param = new object[2] { sender, e };
                this.BeginInvoke(new ConnectNotifyEventHandler(OnConnectNotify), param);
            }
            else
            {
                OnConnectNotify(sender, e);
            }
        }

        /// <summary>
        /// 网络连接完成，开始通讯
        /// </summary>
        protected virtual void OnConnectNotify(object sender, ConnectNotifyEventArgs e)
        {
            if (e.ConnectResult == ConnState.ConnectSuccess)
            {
                GlobalCache.Instance.NetworkMgr.ConnectNotify -= new ConnectNotifyEventHandler(this.ConnectNotify);
                this.ParentForm.ShowProgress("正在进行数据通讯...", true);
                GlobalCache.Instance.TransMgr.Send(TransferType);
                GlobalCache.Instance.TransMgr.TransNotify += new TransactNotifyEventHandler(this.TransactNotify);
            }
            else if (e.ConnectResult == ConnState.NoConnect)
            {
                GlobalCache.Instance.NetworkMgr.ConnectNotify -= new ConnectNotifyEventHandler(ConnectNotify);
                parentForm.HideProgress();
                this.Enabled = true;
                MessageBox.Show("网络连接失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        protected void TransactNotify(object sender, TransNotifyEventArgs e)
        {
            if (this.InvokeRequired)
            {
                object[] param = new object[2] { sender, e };
                this.BeginInvoke(new TransactNotifyEventHandler(this.OnTransNotify), param);
            }
            else
            {
                this.OnTransNotify(sender, e);
            }
        }

        /// <summary>
        /// 上传完成
        /// </summary>
        /// <param name="state"></param>
        public virtual void OnTransNotify(object sender, TransNotifyEventArgs e)
        {
            if (e.TransactStatus != TransStatus.Start && e.TransactType == this.TransferType)
            {
                GlobalCache.Instance.TransMgr.TransNotify -= new TransactNotifyEventHandler(this.TransactNotify);
                this.parentForm.HideProgress();
                this.Enabled = true;
                if (e.TransactStatus == TransStatus.Success)
                {
                    MessageBox.Show("下载成功！");
                }
                else
                {
                    MessageBox.Show("下载失败！");
                }
            }
        }
        #endregion
    }
}